package bairui.international.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.stereotype.Service;

import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.model.domain.jpa.TB_Prize;
import bairui.international.model.domain.jpa.TB_PrizeItem;
import bairui.international.model.status.State_Prize;
import bairui.international.service.Service_Prize;

/**
 * 
 * 【(抽奖)TB_Prize】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_Prize 模型
 * @see Service_Prize 服务接口
 * @see BService_PrizeImpl 服务实现基础
 *
 */
@Service
public class Service_PrizeImpl extends BService_PrizeImpl implements Service_Prize {

	private void validate(TB_Prize bean) throws ValidateObjectException{
		// 验证
		List<TB_PrizeItem> items = bean.getPrizeItems();
		BigDecimal allRate = BigDecimal.ZERO;
		for(TB_PrizeItem item : items){
			if(item.getPrizeName() == null || item.getPrizeName().trim().equals("")){
				throw new ValidateObjectException("object", "", "奖品[名称]数据不完整");
			}
			if(item.getRate() == null){
				throw new ValidateObjectException("object", "", "奖品[中奖率]数据不完整");
			}
			allRate = allRate.add(BigDecimal.valueOf(item.getRate()));
		}
		if(allRate.doubleValue() != 100){
			throw new ValidateObjectException("object", "", "全部奖品的[中奖率]之和必须为100%");
		}
		if(items.size() != 8){
			throw new ValidateObjectException("object", "", "奖品数量必须为8");
		}
	}
	
	@Override
	public void publish(long id, Consumer<TB_Prize> consumer) throws Exception {
		TB_Prize bean = findById(id);
		// 验证
		validate(bean);
		//
		bean.new UpdateProperties() {
			@Override public State_Prize getState(TB_Prize it) throws Exception {
				return it.getState().t_publish_from_Draft().to();
			}
			@Override public Date getPublishTime(TB_Prize it) throws Exception {
				return new Date();
			}
		};
		// 回调
		consumer.accept(bean);
	}

	@Override
	public void up(long id, Consumer<TB_Prize> consumer) throws Exception {
		TB_Prize bean = findById(id);
		// 验证
		validate(bean);
		//
		bean.new UpdateProperties() {
			@Override public State_Prize getState(TB_Prize it) throws Exception {
				return it.getState().t_up_from_Downed().to();
			}
		};		
		// 回调
		consumer.accept(bean);
	}

	@Override
	public void down(long id) throws Exception {
		TB_Prize bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Prize getState(TB_Prize it) throws Exception {
				return it.getState().t_down_from_Uped().to();
			}
		};		
	}

	@Override
	public void forceFinish(long id) throws Exception {
		TB_Prize bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Prize getState(TB_Prize it) throws Exception {
				return it.getState().t_finished_to_Finished().to();
			}
			@Override public Boolean getForceFinishFlag(TB_Prize it) throws Exception {
				return true;
			}           
			@Override public Date getRealFinishTime(TB_Prize it) throws Exception {
				return new Date();
			}
		};			
	}
	
	@Override
	public void autoFinish(long id) throws Exception {
		TB_Prize bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_Prize getState(TB_Prize it) throws Exception {
				return it.getState().t_finished_to_Finished().to();
			}
			@Override public Boolean getForceFinishFlag(TB_Prize it) throws Exception {
				return false;
			}
			@Override public Date getRealFinishTime(TB_Prize it) throws Exception {
				return new Date();
			}
		};			
		bean.setLastModifier(null);
	}

}
